<?php
//keep track of number of questions.
$num = 0;

//returns a list of available templates as an array of strings.
function listTemplates(){
	return array_map("trim", file("../files/templateList.txt"));
}

//readTemplate : String -> Array-of-strings
//takes a file name, opens the file, reads its comments and returns them as an array
function readTemplate($fileName){
	return file("../files/templates/$fileName");
}
//
function readIncomplete($fileName){
	return file("../files/incompleteInspections/$fileName");
}

//parseTemplateLines : Array-of-string -> Array-of-groups
function parseTemplateLines($arrayOfStrings){

	$arrayOfGroups = array();  // format ([groupArray], ...)
	$groupArray = array();  // format ([group Heading], [arrayOfQuestions])
	$arrayOfQuestions = array();//format ([questionArray] , ...)
	$questionArray = array(); //format ([question], [answer], [comment], [photo])
	
	$isG = 0;  // starts at 1 to account for the initial increment	
	$isQ = 0;  //used to determine data for a question. Once the next question is found proceed to new question.
	foreach($arrayOfStrings as $element){
		//If first letter is 'G' and $isEnd is set to one then that means we have a new group.
		if(substr($element, 0, 1)=='G'){
			//is adding $groupArray to $arrayOfGroups
			if($isG){
				$groupArray[1] = $arrayOfQuestions;
				array_push($arrayOfGroups, $groupArray);
				$groupArray = Null;
				$isG = 0;
			}
			else{
				//adding group header to $groupArray at position 0.
				$lineSplit = explode(":", $element);
				$lineData = $lineSplit[1];
				$groupArray[0] = $lineData;
				$isG = 1;
			}
		}
		elseif(substr($element, 0, 1)=="Q"){
			if($isQ){
				array_push($arrayOfQuestions, $questionArray);
				$questionArray = Null;
				$questionArray[0] = $lineSplit[1];
				$isQ = 0;
			}
			else{
				//add question to array
				$lineSplit = explode(":", $element);
				$questionArray[0] = $lineSplit[1];
			}
		elseif(substr($element, 0, 1)=="A"){
			$lineSplit = explode(":", $element);
			$questionArray[1] = $lineSplit[1];
		}
		elseif(substr($element, 0, 1)=="C"){
			$lineSplit = explode(":", $element);
			$questionArray[2] = $lineSplit[1];
		}elseif(substr($element, 0, 1)=="P"){
			$lineSplit = explode(":", $element);
			$questionArray[3] = $lineSplit[1];
		}
	}
	array_push($arrayOfQuestions, $questionArray);
	$questionArray = Null;
	$groupArray[1] = $arrayOfQuestions;
	array_push($arrayOfGroups, $groupArray);
	$groupArray = Null;
	
	return $arrayOfGroups;
}
//takes in $arrayOfGroups = ([groupArray], ...)
function loadTemplate($arrayOfGroups){
	foreach($arrayOfGroups as $groupArray){
		renderGroup($groupArray);
	}
}
//takes $groupArray = ([groupHeading], [arrayOfQuestions])
function renderGroup($groupArray){
	echo "<div class=\"heading\">$groupArray[0]</div>";
	echo "<ol>";
	foreach($groupArray[1] as $question){
		renderQuestion($question);
	}
	echo "</ol>";
}
// takes $question = ([question], [answer], [comment], [photo])
function renderQuestion($question){
	global $num;
	echo "<li> 
			<p> $string </p>
			<span>
				<input type=\"radio\" name=\"$num\" id=\"y$num\" value=\"y\"/><label for=\"y$num\">Yes</label>
				<input type=\"radio\" name=\"$num\" id=\"n$num\" value=\"n\"/><label for=\"n$num\">No</label>
				<input type=\"radio\" name=\"$num\" id=\"_$num\" value=\"N/A\"/><label for=\"_$num\">N/A</label>
			</span>
			<span>
				<button>Add Comment</button>
				<button>Add Photo</button>
			</span>
		</li>";
	$num++;
}
//takes $answer = $string. either(y, n, or _) if exists.
renderRadio($answer){

}
//takes $comment = $string. supplied string for user if exists.
renderComment($comment){

}

/*
//returns html that would populate the U.I. with the given protocol.
function loadTemplate($fileName){
	$protocol = readTemplate($fileName); //array of strings
	$groups = parseTemplateLines($protocol); // returns an array of groups given the protocol
	
	$isEnd = 0; 	//variable to determine when to close the ordered html list.
	$num = 1;
	foreach($protocol as $line){
		$firstLetter = substr($line, 0, 1);
		//echo $firstLetter;
		$lineSplit = explode(":", $line);
		$lineData = $lineSplit[1];

		if($firstLetter == "G"){
			if($isEnd == 1){
				echo "</ol>";
				$isEnd = 0;
			}
			echo "<div class=\"$lineData\">$lineData</div>";
			echo "<ol>";
		}
		else{
			$isEnd = 1;
			echo "<li> 
					<p> $lineData </p>
					<span>
						<input type=\"radio\" name=\"$num\" id=\"y$num\" value=\"y\"/><label for=\"y$num\">Yes</label>
						<input type=\"radio\" name=\"$num\" id=\"n$num\" value=\"n\"/><label for=\"n$num\">No</label>
						<input type=\"radio\" name=\"$num\" id=\"_$num\" value=\"N/A\"/><label for=\"_$num\">N/A</label>
					</span>
					<span>
						<button>Add Comment</button>
						<button>Add Photo</button>
					</span>
				</li>";
			$num++;
		}
	}
}

renderGroup : Group -> HTMLString

renderQuestion : String -> HTMLString

renderTemplate : Array-of-groups -> HTMLString


g = array( "header" => "Fire Safety",
           "items" => array( "Is there a fire extinguisher?",
		                     "Are burners turned off?",
							 "Electrical sockets grounded") )

assertEqual(  renderQuestion("Is there a fire extinguisher?"),
              "<li><p>Is there a fire extinguisher?</p>$YESLABEL $NOLABEL ...")
*/

function test($input, $output){
	if($input===$output) {
		echo "passed";
	} else {
		echo "FAILURE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
	}
}

?>